'''
Created on Jan 21, 2011

@author: oabalbin
'''

import os
import subprocess
from collections import defaultdict, deque
from optparse import OptionParser

from exome.jobs.base import JOB_SUCCESS, JOB_ERROR
from exome.jobs.job_runner import qsub_cac, qsub_loc, run_local


NODE_MEM=45000.0
NODE_CORES=12
SINGLE_CORE=1
MEM_PER_CORE= int(float(NODE_MEM) / NODE_CORES)
# wt=walltime
WT_SHORT= "24:00:00"
WT_LONG= "60:00:00" #"100:00:00"


def create_snps_annotation(input_vcf_file, output_vcf_file, output_vcf_log,
                           reference_genome, genelist_annotation, path_to_vcfCodSNPs):
    '''
    path_to_vcfCodSNPs/vcfCodingSnps.v1.5 -s ./t.filtered.hard.vcf -r 
    /human_g1k_v37.fasta -g 
    /exds/users/oabalbin/sw/vcfCodingSnps.v1.5/geneLists/CCDSGene.B37.txt 
    -o ./vcfCodingSNP.out.vcf -l ./vcfCodingSNP.log
    '''
    vcf_command=path_to_vcfCodSNPs+'vcfCodingSnps'
    args = [vcf_command, '-s', input_vcf_file, '-r',reference_genome,
            '-g', genelist_annotation,'-o', output_vcf_file, '-l',output_vcf_log] #output_vcf_log

    return args

    
if __name__ == '__main__':
    
    optionparser = OptionParser("usage: %prog [options] ")
    optionparser.add_option("-s", "--input_file", dest="input_file",
                            help="vcf input file with the called snps")
    optionparser.add_option("-o", "--output_file", dest="output_file",
                            help="output file to store the annotated snps") 
    optionparser.add_option("-r", "--reference_genome", dest="reference_genome",
                            help="reference_genome") 
    optionparser.add_option("-g", "--annotated_genelist", dest="annotated_genelist",
                            help="annotated_genelist") 

    optionparser.add_option("--local", dest="local", action="store_true", default=False)
    optionparser.add_option("--cluster", dest="cluster", action="store_true", default=False)
    optionparser.add_option("-p", "--processes", type=int, dest="num_processors", default=1)

    (options, args) = optionparser.parse_args()    

    
    if not (options.local ^ options.cluster):
        optionparser.error("Must set either --local or --cluster to run job")
    if options.local:
        jobrunfunc = run_local
    elif options.cluster:
        jobrunfunc = qsub
    
    path_to_vcfCodSNPs='/exds/users/oabalbin/sw/vcfCodingSnps.v1.5/'
    
    output_vcf_log = options.output_file.replace('vcf','log')
    cmd = create_snps_annotation(options.input_file, options.output_file, output_vcf_log,
                           options.reference_genome, options.annotated_genelist, 
                           path_to_vcfCodSNPs)
    
    jobn="ann"
    my_email='alebalbin@gmail.com'
    jobrunfunc(jobn+'_bf', cmd, SINGLE_CORE, cwd=None, walltime=WT_SHORT, pmem=MEM_PER_CORE, 
                                  deps=None, stdout=None, email_addresses=my_email)




